package com.liuyanzhao.ssm.blog.controller.admin;


import com.liuyanzhao.ssm.blog.entity.App;
import com.liuyanzhao.ssm.blog.entity.Link;
import com.liuyanzhao.ssm.blog.entity.User;
import com.liuyanzhao.ssm.blog.enums.UserRole;
import com.liuyanzhao.ssm.blog.service.AppService;
import com.liuyanzhao.ssm.blog.service.UserService;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.text.DateFormat;
import java.text.Format;
import java.text.SimpleDateFormat;
import java.util.*;


/**
 * @author liuyanzhao
 */
@Controller
@RequestMapping("/admin/app")
public class BackAppController {

    @Autowired
    private AppService appService;

    @RequestMapping(value = "")
    public ModelAndView apprList() {
        ModelAndView modelandview = new ModelAndView();
        List<App> appList = appService.listApp("aaa");
        modelandview.addObject("appList", appList);
        modelandview.setViewName("Admin/App/index");
        return modelandview;
    }


    @RequestMapping(value = "/edit/{id}")
    public ModelAndView editLinkView(@PathVariable("id") Integer id)  {
        ModelAndView modelAndView = new ModelAndView();

        App pojo = appService.queryById(id);
        modelAndView.addObject("pojo",pojo);

        List<App> appList = appService.listApp(null);
        modelAndView.addObject("appList",appList);

        modelAndView.setViewName("Admin/App/edit");
        return modelAndView;
    }

    @RequestMapping(value = "/editSubmit",method = RequestMethod.POST)
    public String editLinkSubmit(App app)  {
        Date date = new Date();
        DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        app.setUpdateTime(format.format(date));
        appService.update(app);
        return "redirect:/admin/app";
    }

    @RequestMapping(value = "/insert")
    public ModelAndView insertLinkView()  {
        ModelAndView modelAndView = new ModelAndView();

        List<App> appList = appService.listApp(null);
        List<String> yyList = new ArrayList<>();
        yyList.add("虎牙");
        yyList.add("斗鱼");

        List<String> csList = new ArrayList<>();
        csList.add("电子现金总额");
        csList.add("单笔消费限额");
        modelAndView.addObject("appList",appList);
        modelAndView.addObject("yyList",yyList);
        modelAndView.addObject("csList",csList);
        modelAndView.setViewName("Admin/App/insert");
        return modelAndView;
    }

    @RequestMapping(value = "/insertSubmit",method = RequestMethod.POST)
    public String insertLinkSubmit(App app)  {
        Date date = new Date();
        Format format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        app.setCreateTime(format.format(date));
        app.setUpdateTime(format.format(date));
        app.setCsStatus("可用");
        appService.insert(app);
        return "redirect:/admin/app/insert";
    }

    @RequestMapping(value = "/delete/{id}")
    public String deleteLink(@PathVariable("id") Integer id)  {
        appService.deleteById(id);
        return "redirect:/admin/app";
    }


}
